import Vue from 'vue'
import { getLoginStatus, getLikelist, getuserDetail, getUserAccount, getLogout, getUserSubcount, getUserLevel, getUserBinding, getUserPlaylist } from "@/api/User"
import router from "@/router"
const actions = {
  // 获取登录状态
  async getAcount ({ commit }) {
    let res = await getLoginStatus()
    if (res.data.code === 200) {
      commit('GETACOUNT', res.data.profile)
      window.localStorage.setItem('getAcount', JSON.stringify(res.data.profile))
      // 更新登录状态（isLogin）
      commit('UPDATEDISLOGIN', true)
      let uid = res.data.profile.userId
      // 获取用户信息
      let userDetail = await getuserDetail(uid)
      // 获取用户账号详情
      let userAccount = await getUserAccount()
      window.sessionStorage.setItem('userAccount', JSON.stringify(userAccount))
      window.sessionStorage.setItem('userDetail', JSON.stringify(userDetail))
      // 获取用户信息 , 歌单，收藏，mv, dj 数量
      let userSubcount = await getUserSubcount()
      state.userSubcount = userSubcount
      // 获取用户等级信息
      let userLevel = await getUserLevel()
      state.userLevel = userLevel
      // 获取用户绑定信息
      let userBinding = await getUserBinding(uid)
      state.userBinding = userBinding
      // 获取用户歌单
      let userList = await getUserPlaylist(uid)
      state.id = userList.playlist[0].id
    }
  },
  // 退出登录
  getLogout ({ commit }) {
    if (!state.isLogin) return Vue.prototype.$message.warning('似乎并没有登录')
    Vue.prototype.$confirm('将要退出登录, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(async () => {
        let res = await getLogout()
        if (res.code === 200) {
          commit('GETACOUNT', { getAcount: null })
          window.localStorage.removeItem('getAcount')
          window.localStorage.removeItem('isLogin')
          commit('UPDATEDISLOGIN', false)
          router.push('/')
        }
      })
      .catch((err) => {
        console.log(err);
        Vue.prototype.$message({
          type: 'info',
          message: '已取消'
        })
      })
  },
  // 获取用户喜欢歌曲
  async getLikelist () {
    if (window.localStorage.getItem('getAcount')) {
      var getAcount = JSON.parse(localStorage.getItem('getAcount'))
      var userid = getAcount.userId
      let res = await getLikelist(userid)
      if (res.code !== 200) return
      state.likeList = res.ids
    }
  },
}
const mutations = {
  GETACOUNT (state, value) {
    state.getAcount = value
  },
  UPDATEDISLOGIN (state, value) {
    state.isLogin = value
    window.localStorage.setItem('isLogin', value)
  },
  // 用户喜欢列表
  UpLikelist (state, value) {
    state.likeList = value
  },
  // 添加或删除用户喜欢歌曲
  setLikeList (state, updata) {
    if (updata.type === 'unshift') {
      state.likeList.unshift(updata.data)
    } else if (updata.type === 'remove') {
      state.likeList.splice(
        state.likeList.indexOf(updata.data), 1
      )
    } else if (updata.type === 'get') {
      state.likeList = updata.likeList
      console.log(state.likeList, '2000')
    }
  }
}
const state = {
  getAcount: {},//用户信息
  uid: '',//用户id
  isLogin: false,//是否登录
  userAccount: {},//用户账号详情
  userSubcount: {},// 获取用户信息 , 歌单，收藏，mv, dj 数量
  userLevel: {},//用户等级信息
  userBinding: {},//用户绑定信息
  id: [],//用户歌单id,
  likeList: [],//用户喜欢歌曲idlist

}
export default {
  actions,
  mutations,
  state
}